package com.javastudy.forum.service.impl;

import com.javastudy.forum.common.AppResult;
import com.javastudy.forum.common.ResultCode;
import com.javastudy.forum.dao.MessageMapper;
import com.javastudy.forum.exception.ApplicationException;
import com.javastudy.forum.model.Message;
import com.javastudy.forum.model.User;
import com.javastudy.forum.service.IMessageService;
import com.javastudy.forum.service.IUserService;
import com.javastudy.forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Chooker
 * @create 2023-08-24 11:10
 */
@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
        if (message == null || StringUtils.isEmpty(message.getContent()) || message.getPostUserId() == null || message.getReceiveUserId() == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接受者
        User user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeletestate() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        message.setCreateTime(new Date());
        message.setUpdateTime(new Date());
        //0状态表示未读
        message.setState((byte) 0);

        //发送消息
        int row = messageMapper.insertSelective(message);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public Long selectUnreadCount(Long userId) {
        if (userId == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = userService.selectById(userId);
        if (user == null || user.getDeletestate() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Long num = messageMapper.selectByNum(userId);
        return num;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if (receiveUserId == null || receiveUserId < 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        // 非空校验, state : 0 未读 1 已读 2 已回复
        if (id == null || state == null || id < 0 || state < 0 || state > 2) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = new Message();
        message.setId(id);
        message.setState(state);
        message.setUpdateTime(new Date());
        int row = messageMapper.updateByPrimaryKeySelective(message);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public Message selectById(Long id) {
        if (id == null || id < 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if (repliedId == null || repliedId < 0 || message == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message repliedMessage = selectById(repliedId);
        if (repliedMessage == null || repliedMessage.getDeleteState() == 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新状态
        updateStateById(repliedId, (byte) 2);
        //发送信息
        create(message);
    }
}
